candidates = [10, 1, 2, 7, 6, 1, 5], target = 8


var combinationSum2 = function (candidates, target) {
    candidates.sort((a, b) => a - b)
    let result = []
    let zuhe = []
    let len = candidates.length
    let used = new Array(len).fill(0)
    let sum = 0

    function dfs(index) {
        if (sum > target) {
            return;
        }
        if (sum === target) {
            result.push(zuhe.slice())
            return;
        }

        for (let i = index; i < len; i++) {
            if (i > 0 && candidates[i] === candidates[i - 1] && used[i - 1] === 0) {
                continue;
            }
            zuhe.push(candidates[i]);
            sum += candidates[i]
            used[i] = 1
            dfs(i + 1)
            zuhe.pop()
            sum -= candidates[i]
            used[i] = 0
        }
    }

    dfs(0)
    return result
};

console.log(combinationSum2(candidates, target));
